package com.mDigitalHealth.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.mDigitalHealth.R;
import com.mDigitalHealth.common.Utils;

public class VideoViewActivity extends Activity implements OnErrorListener, OnCompletionListener {

	public static final String TAG = "VideoPlayer";

	private VideoView mVideoView;

	private Uri mUri;

	private int mPositionWhenPaused = -1;

	private MediaController mMediaController;

	private ProgressDialog waitDialog;

	private boolean netSataus = false;

	// private ProgressBar progressBar;

	/* (non-Javadoc)

	* @see android.app.Activity#onCreate(android.os.Bundle)

	*/

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(R.layout.videoview);
		// progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);

		mVideoView = (VideoView) findViewById(R.id.videoview);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 竖屏

		// 文件路径 http://www.pocketjourney.com/downloads/pj/video/famous.3gp
		// String url = Environment.getExternalStorageDirectory() + "/video.3gp";
		// String url = Environment.getExternalStorageDirectory() + "/I Will Always Love You.mp3";
		// String url = "http://wangjun.easymorse.com/wp-content/video/mp4/tuzi.3gp";

		Intent intent = getIntent();
		// String path = intent.getStringExtra("path");
		String path = Utils.VIDEO_URL;
		Log.d(this.getClass().getName(), "Path:" + path);
		mUri = Uri.parse(path);

		mVideoView.setOnPreparedListener(new videoPrepared());
		// Create media controller
		mMediaController = new MediaController(this);

		// 设置MediaController
		mVideoView.setOnErrorListener(this);
		mVideoView.setOnCompletionListener(this);
		mVideoView.setMediaController(mMediaController);
		// mVideoView.start();

		// waitDialog = ProgressDialog.show(this, "视频加载中...", "请您稍候");

		waitDialog = new ProgressDialog(this);
		waitDialog.setMessage("请您稍候");
		waitDialog.setTitle("视频加载中...");

		netSataus = netWorkStatus();
		if (netSataus == true) {
			setPlayURL();
		}
	}

	public void setPlayURL() {
		mVideoView.setVideoURI(mUri);
		mVideoView.requestFocus();
		waitDialog.show();
	}

	private boolean netWorkStatus() {

		boolean netSataus = false;
		ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

		cwjManager.getActiveNetworkInfo();

		if (cwjManager.getActiveNetworkInfo() != null) {
			netSataus = cwjManager.getActiveNetworkInfo().isAvailable();
		}

		if (!netSataus) {
			AlertDialog.Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage(
					"是否对网络进行设置？如果选择否将无法播放");
			b.setPositiveButton("是", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					Intent mIntent = new Intent("/");
					ComponentName comp = new ComponentName("com.android.settings",
							"com.android.settings.WirelessSettings");
					mIntent.setComponent(comp);
					mIntent.setAction("android.intent.action.VIEW");
					// 设置主函数回调值
					startActivityForResult(mIntent, 1); // 如果在设置完成后需要再次进行操作，可以重写操作代码，在这里不再重写

				}
			}).setNeutralButton("否", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					dialog.cancel();

				}
			}).show();
		}

		return netSataus;
	}

	/**
	 * dialog回调函数，根据返回值确定是否需要重新加载网站
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1) {
			setPlayURL();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	// 监听MediaPlayer上报的错误信息

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {

		System.out.println("onError");
		return false;

	}

	// Video播完的时候得到通知

	@Override
	public void onCompletion(MediaPlayer mp) {
		// 播放完成提示
		Toast.makeText(VideoViewActivity.this, "video play finished!", Toast.LENGTH_LONG);

		this.finish();

	}

	// 开始

	public void onStart() {

		// dialog.dismiss();
		// Play Video

		// mVideoView.setVideoURI(mUri);
		// mVideoView.requestFocus();
		mVideoView.start();

		super.onStart();

	}

	// 暂停

	public void onPause() {

		// Stop video when the activity is pause.

		mPositionWhenPaused = mVideoView.getCurrentPosition();

		mVideoView.stopPlayback();

		Log.d(TAG, "OnStop: mPositionWhenPaused = " + mPositionWhenPaused);

		Log.d(TAG, "OnStop: getDuration = " + mVideoView.getDuration());

		super.onPause();

	}

	public void onResume() {

		// Resume video player

		if (mPositionWhenPaused >= 0) {

			mVideoView.seekTo(mPositionWhenPaused);

			mPositionWhenPaused = -1;

		}

		super.onResume();

	}

	class videoPrepared implements OnPreparedListener {

		@Override
		public void onPrepared(MediaPlayer mp) {

			waitDialog.dismiss();
			// progressBar.setVisibility(View.GONE);

		}
	}

}
